package java.com.codingmanage.dao.hibernate;

import java.com.codingmanage.dao.interfac.BaseDAO;
import java.com.codingmanage.model.base.Model;
import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateDAO implements BaseDAO {

	Session session;
	SessionFactory sessionFactory=SessionFactoryUtils.getInvocation().getSessionFactory();
	
	@Override
	public Long save(Model obj) {
		session=sessionFactory.openSession();
		Transaction t=session.beginTransaction();
		t.begin();
		Serializable id=session.save(obj);
		t.commit();
		session.close();
		System.out.println( id );
		return (Long) id;
	}

	@Override
	public Long update(Model obj) {
		Long id=obj.getId();
		session=sessionFactory.openSession();
		Transaction t=session.beginTransaction();
		t.begin();
		session.update(obj);
		t.commit();
		session.close();
		System.out.println( id );
		return (Long) id;
	}

	@Override
	public Long delete(Model obj) {
		Long id=obj.getId();
		session=sessionFactory.openSession();
		Transaction t=session.beginTransaction();
		t.begin();
		session.delete(obj);
		t.commit();
		session.close();
		System.out.println( id );
		return (Long) id;
	}

	@Override
	public Long get(Model obj) {
		Long id=obj.getId();
		session=sessionFactory.openSession();
		Transaction t=session.beginTransaction();
		t.begin();
		Criteria criteria=session.createCriteria(obj.getClass());
		criteria.list();
		t.commit();
		session.close();
		System.out.println( id );
		return (Long) id;
	}

	@Override
	public List findAll() {
		// TODO Auto-generated method stub
		return null;
	}

}
